﻿using Dot5Demo.MarkDown;
using Dot5Demo.MarkDowngm;
using Markdig;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Dot5Demo
{
    public partial class FrmMarkDown : Form
    {
        public StringBuilder _builder;
        public string CssStyle;
        public string htmlTemplate1 = "<html><head><style type=\"text/css\">{0}</style></head><body>{1}</body></html>";
        public string htmlTemplate2 = "<html><head></head><body>{0}</body></html>";
        public int Cols { get; set; }
        public double[,] Data { get; set; }
        public int Rows { get; set; }

        private MDGrid grid;

        public FrmMarkDown()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;

            this.webBrowser1.ScriptErrorsSuppressed = false;
            CssStyle = MarkDown.CSSConsts.DefaultCss;
            this.Shown += FrmMarkDown_Shown;

            grid = new MDGrid();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var mdTxt = _builder.ToString();
            var res = Markdig.Markdown.ToHtml(mdTxt);
            var html = string.Format(htmlTemplate1, CssStyle, res);
            this.webBrowser1.DocumentText = html;
            //this.webBrowser1.Refresh();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Clipboard.Clear();
            Clipboard.SetDataObject(_builder.ToString());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();
            var res = Markdig.Markdown.ToHtml(_builder.ToString(), pipeline);
            var html = string.Format(htmlTemplate1, CssStyle, res);
            this.webBrowser1.DocumentText = html;
        }

        private void FrmMarkDown_Shown(object sender, EventArgs e)
        {
            var builder = new StringBuilder();
            builder.Append("|");
            for (int i = 0; i < Cols; i++)
            {
                var cl = string.Format(@"$\alpha 列:{0}$", i);
                builder.Append(cl + "|");
                //添加列
                grid.AddCol(cl);
            }
            builder.Append('\n');
            builder.Append("|");
            for (int i = 0; i < Cols; i++)
            {
                builder.Append("----|");
            }
            builder.Append('\n');
            for (int j = 0; j < Rows; j++)
            {
                builder.Append("|");
                var row = grid.NewRow();
                for (int i = 0; i < Cols; i++)
                {
                    var txt = string.Format("{0}", Data[i, j].ToString());
                    builder.Append(txt + "|");
                    row[i].Text = txt;
                }
                builder.Append('\n');
                grid.AddRow(row);
            }
            _builder = builder;
        }

        public static string HtmlFile = "2022.html";
        public static string TestMathF = @"
$$
{y'\Big|_{x=x_0} \quad or \quad \frac{dy}{dx} \Big|_{x=x_0} \quad or \quad \frac{df(x)}{dx} \Big|_{x=x_0} }
$$
<br>
";
        private void button4_Click(object sender, EventArgs e)
        {
            var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().UseMathematics().UseGridTables().Build();
            var mdStr = grid.GenerateStr();
            var htmlstr = Markdown.ToHtml(mdStr, pipeline);
            htmlstr = TestMathF + htmlstr;
            var html = MarkDownDoc.WrapHtmlText(htmlstr, CSSConsts.DefaultCss);
            var writer = File.CreateText(HtmlFile);
            writer.Write(html);
            writer.Flush();
            writer.Close();
            this.webBrowser1.DocumentText = html;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string url = Path.Combine(Directory.GetCurrentDirectory(), HtmlFile);
            //Process process = Process.Start("chrome.exe", url);
            if (File.Exists(url))
            {
                if (Environment.Version.Major == 4)
                {
                    Process.Start("chrome.exe", url);
                }
                else
                {
                    var fdiag = new OpenFileDialog()
                    {
                        Filter = "chrome浏览器(*.exe)|*.exe",
                        Title = "选择浏览器",
                    };
                    if (fdiag.ShowDialog() == DialogResult.OK)
                    {
                        Process.Start(fdiag.FileName, url);
                    }
                }
            }
            else
                MessageBox.Show("文件不存在：" + url);
        }
    }
}